package com.haodai.vip.tomcat.server;

import com.haodai.vip.tomcat.utils.DateUtils;
import com.haodai.vip.tomcat.utils.GzipUtils;
import com.haodai.vip.tomcat.utils.LogUtils;

import java.io.*;
import java.util.*;

public class Response {

    private OutputStream output;
    private List<Byte> responseBody = new ArrayList<>();
    private Map<String, String> headerMap = new HashMap<>();
    private List<Cookie> cookieList = new ArrayList<>();

    public Response(OutputStream output) {
        this.output = output;

        headerMap.put("Content-Type", "text/html;charset="+Config.Encoding.ENCODE);
    }

    public void write(byte[] content){
        for (int i = 0; i < content.length; i++) {
            responseBody.add(content[i]);
        }
    }

    public void write(String content){
        try {
            byte[] bytes = content.getBytes(Config.Encoding.ENCODE);
            for (int i = 0; i < bytes.length; i++) {
                responseBody.add(bytes[i]);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public byte[] getResponseBody(){
        byte[] chars = new byte[responseBody.size()];
        for (int i = 0; i < responseBody.size(); i++) {
            chars[i] = responseBody.get(i);
        }
        return chars;
    }

    public void out() throws Exception{
        String content = new String(getResponseBody(), Config.Encoding.ENCODE);
        byte[] contentBytes = content.getBytes(Config.Encoding.ENCODE);

        String responseMessage = "HTTP/1.1 200 ok\r\n";


        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            responseMessage+=entry.getKey()+": "+entry.getValue()+"\r\n";
        }

        responseMessage+="Content-Length: "+contentBytes.length+"\r\n";
        Date date = new Date();
        for (Cookie cookie : cookieList) {
            responseMessage+="Set-Cookie: "+cookie.name+"="+ cookie.value +"; Expires="+ DateUtils.getGMT(date.getTime(), cookie.delay)+"\r\n";
        }

        responseMessage += "\r\n" +content;

        output.write(responseMessage.getBytes(Config.Encoding.ENCODE));
    }

    public void gzip() throws Exception{
        String content = new String(getResponseBody(), Config.Encoding.ENCODE);

        byte[] contentBytes = GzipUtils.zip(content);

        String responseMessage = "HTTP/1.1 200 ok\r\n";

        for (Map.Entry<String, String> entry : headerMap.entrySet()) {
            responseMessage+=entry.getKey()+": "+entry.getValue()+"\r\n";
        }

        responseMessage+="Content-Length: "+contentBytes.length+"\r\n";
        responseMessage+="Content-Encoding: gzip\r\n";

        Date date = new Date();
        for (Cookie cookie : cookieList) {
            if (cookie.delay != 0){
                responseMessage+="Set-Cookie: "+cookie.name+"="+ cookie.value +"; Expires="+ DateUtils.getGMT(date.getTime(), cookie.delay)+"\r\n";
            } else {
                responseMessage+="Set-Cookie: "+cookie.name+"="+ cookie.value +"; Path=/; HttpOnly\r\n";
            }
        }

        responseMessage += "\r\n";

        output.write(responseMessage.getBytes(Config.Encoding.ENCODE));
        output.write(contentBytes);
    }

    public void addCookie(String name, String value, int delay){
        cookieList.add(new Cookie(name, value, delay*1000));
    }

    public void addCookie(String name, String value){
        cookieList.add(new Cookie(name, value));
    }
}